<!--
 *  Description: System sends an email reminder to all members who signed up for the upcoming events that were selected by administrator
 *               and shows the result of sending email.
 -->

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<jsp:useBean id="event" scope="session" class="com.asaga.tools.Event_DBImpl" />
<jsp:useBean id="userProfile" scope="session" class="com.asaga.tools.SystemClient_DBImpl" />
<jsp:useBean id="emailBean" scope="session" class="com.asaga.tools.Email_DBImpl" />
<jsp:useBean id="fontBean" scope="session" class="com.asaga.tools.FontBean" />
<jsp:setProperty name="fontBean" property="*" />
<jsp:setProperty name="emailBean" property="*" />

<%@page import="java.sql.*, java.io.*, java.util.*, java.net.*"%>

<!-- IMPORT from Java Mail API -->
<%@page import="javax.mail.*, javax.mail.internet.*, javax.activation.*"%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>The Status of Sending Email Reminders</title>
        <%@ include file="header.jsp" %>
        <%
        Vector toAll = new Vector();
        String mailTo = null;
        // DECLARE email server
        String SMTP_HOST = "localhost";
	String mailFrom = "administrator@localhost";
        // email reminder's subject
	String mailtitle = "Event Reminder from ASA-GGC";
        // email reminder's body
	String mailbody = "Dear Member:<p> The event you register at is coming.<p> Please check the event date and location.<p> Thank you.<p> ASA-GGC<p>";
	String sendmail = " Subject : " + mailtitle + " " + " <br><hr> " +  " " + mailbody + " " + " <hr> " + " </font> ";

	// To create session, java.util.Properties class is created.
	// SMTP host address is assigned
	Properties props = new Properties();
	props.put("mail.smtp.host", SMTP_HOST);
	
	// Create and assign of mail session
	Session msgSession = Session.getDefaultInstance(props, null);

    	ResultSet FnameEmails = emailBean.getAllEmailForReminders();
	%>
    </head>
    
<body>  
    <div id="container">
            <%@ include file="topNav.jsp" %>
            <%@ include file="leftside.jsp" %>
            <%@ include file="rightside.jsp" %>
    <div id="content">
<table border="0" width="561">
    <tr>
        <td width="555">
            <p><font>&nbsp;</font><font face="Verdana" size="3"><b>The Status of Sending Email</b></font></p>
        </td>
    </tr>    
</table>

     <% if (FnameEmails != null){

        while (FnameEmails.next()) {
        String emailAddress = FnameEmails.getString("email");

        mailTo = emailAddress;
        mailTo = mailTo.trim();

        // PRINT the result of sending emails
        out.println("Successfully " +mailTo+" was sent.<br>");

        try {

        // Create object of message class using session
        MimeMessage msg = new MimeMessage(msgSession);
        InternetAddress from = new InternetAddress(mailFrom);
        msg.setFrom(from);

        InternetAddress to = new InternetAddress(mailTo);
        msg.setRecipient(Message.RecipientType.TO, to);

        msg.setSubject(mailtitle);

        msg.setContent(sendmail, "text/html; charset=EUC_KR"); // HTML Type

        // speaks the protocol specific language for sending the message
        Transport.send(msg);

        } // end of try

        catch (AddressException mex) { out.println("The following email address has a problem:" +mailTo+"<br>"); } 

        catch (SendFailedException mex) { out.println("The following email address failed to send:" +mailTo+"<br>"); } 

        catch (MessagingException mex) { mex.printStackTrace(); } 

        } // end of while
        %>
        
        <form action="index.jsp" method="get">                
        <input type="submit" name="action" value="Go to the Main">
        </form>   
        
        <% 
        } // end of if
        
        else {                
        //If there is no email list, then display message and "Go to the Main"                
        out.println("<h3>There is no email list.</h3>");
        %>
        <form action="index.jsp" method="get">                
        <input type="submit" name="action" value="Go to the Main">
        </form>                 
        <% } %>              

        </div>
            <%@ include file="footer.jsp" %>
        </div>
</body>
</html>